<!--
INPUT
    $messages :
        array (topic => array(msg_parent, msg_reply, , , , )
        topic is msg_master id
        msg_parent and msg_reply are all msg_master ORM
    $user : currently accessing user
-->

<div class="wallContent" id="wallContent">
    <!-- Compose Title -->
    <div class="upload_title" onclick="toggleVisibility(document.getElementById('compose_form')); toggleArrowBtn(document.getElementById('arr_compose'));">
        <img src="<?php echo URL::base_url() . 'public/image/arrow_collapse.gif' ?>" alt="" id="arr_compose"/>
        Compose Message
    </div>

    <!-- Compose Form -->
    <form id="compose_form" class="dark_form compose_form" action="" style="display: none" method="post">
        <table>
            <tr>
                <td class="leftmost">Title</td>
                <td>:</td>
                <td><input type="text" name="title"/></td>
            </tr>
            <tr>
                <td class="leftmost">To</td>
                <td>:</td>
                <td><input type="text" name="to"/></td>
            </tr>
            <tr>
                <td class="leftmost">Content</td>
                <td>:</td>
                <td><textarea cols="" rows="" name="content"></textarea></td>
            </tr>
            <tr>
                <td class="leftmost"></td>
                <td></td>
                <td>
                    <input type="hidden" name="key" value="send msg" />
                    <input class="isubmit" type="submit" value="Send!" />
                </td>
            </tr>
        </table>
    </form>

    <?php foreach($messages as $topicid => $msg) { ?>
        <div class="message">
            <?php
                //Echo msg header
                $topic = new ORM('msg_master');
                $topic->find($topicid);
                $msg_header_view = View::factory('message/msgheader');
                $msg_header_view->model = $topic;
                $msg_header_view->i = $topic->id;
                echo $msg_header_view;
            ?>
            <div class="msg_content_wrapper" id="msg_content<?php echo $topic->id ?>" style="display: none">
            <?php
                //Echo content and replies
                $msgcontent_view = View::factory("message/msgcontent");
                foreach ($msg as $content) {
                    $msgcontent_view->model = $content;
                    echo $msgcontent_view;
                }

                //Echo form
                $replyform_view = View::factory("message/replyform");
                $replyform_view->model = $topic;
                $replyform_view->user  = $user;
                echo $replyform_view;
            ?>
            </div>
        </div>
    <?php } ?>

    <!-- Prev Next -->
    <div class="prev_next">
        <?php if(isset ($prevpage)) { ?>
        <div class="prevbutton">
            <a onclick="getAnotherPageMessage(<?php echo $prevpage; ?>)" href="">
                <img src="<?php echo URL::base_url() . '/public/image/left.png' ?>" alt="Prev"/>
            </a>
        </div>
        <?php } ?>
        <?php if(isset($nextpage)) { ?>
        <div class="nextbutton">
            <a onclick="getAnotherPageMessage(<?php echo $nextpage; ?>)" href="">
                <img src="<?php echo URL::base_url() . '/public/image/right.png' ?>" alt="Next"/>
            </a>
        </div>
        <?php } ?>
    </div>
</div>